package logic

import (
	"context"
	"fmt"
	"good/cache"
	"good/models"
	"time"

	"good/goodrpc/goodrpc"
	"good/goodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodShowLogic {
	return &GoodShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodShowLogic) GoodShow(in *goodrpc.GoodShowRequest) (*goodrpc.GoodShowResponse, error) {
	// todo: add your logic here and delete this line

	var good models.Good
	models.DB.Debug().Where("id = ?", in.GoodId).Find(&good)
	if good.ID == 0 {
		return nil, fmt.Errorf("查询失败")
	}

	cache.Rdb.Set(l.ctx, "goodShow", good, time.Minute*120)

	return &goodrpc.GoodShowResponse{
		Title:   good.Title,
		Price:   float32(good.Price),
		Num:     good.Num,
		ClassId: good.ClassId,
		GoodId:  int64(good.ID),
	}, nil
}
